/**
* 版权所有：福建邮科电信业务部厦门研发中心 
*====================================================
* 文件名称: JsonSupport.java
* 修订记录：
* No    日期				作者(操作:具体内容)
* 1.    Jun 16, 2012			詹保山(softzbs@126.com)(创建:创建文件)
*====================================================
* 类描述：(说明未实现或其它不应生成javadoc的内容)
* 
*/
package com.devframe.web.handle;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;
import net.sf.json.util.JSONUtils;

import com.devframe.web.JSONPropertyFilter;
import com.devframe.web.MyContext;

/**
 *<pre><b><font color="blue">JsonSupport</font></b></pre>
 *
 *<pre><b>&nbsp;--描述说明--</b></pre>
 * <pre>JSON的操作</pre>
 * <pre>
 * <b>--样例--</b>
 *   JsonSupport obj = new JsonSupport();
 *   obj.method();
 * </pre>
 */
public class JSONResultHandle {
	private static final Logger LOG = Logger.getLogger(JSONResultHandle.class);
    private static final JsonConfig JSONCOFIG = new JsonConfig();
    private static final JSONPropertyFilter JSON_FILTER = new JSONPropertyFilter();
    
    static{
    	   JSONCOFIG.setCycleDetectionStrategy(CycleDetectionStrategy.NOPROP);
           JSONCOFIG.addIgnoreFieldAnnotation(java.sql.Connection.class);
           JSONCOFIG.addIgnoreFieldAnnotation(java.sql.Statement.class);
//           JSONCOFIG.addIgnoreFieldAnnotation(org.jwebap.plugin.tracer.jdbc.ProxyStatement.class);
           JSONCOFIG.setJsonPropertyFilter(JSON_FILTER);
    }
    
    
    public void execute(HttpServletResponse response,Object data){
   	 // 返回对象类型当做AJAX处理。
       response.setContentType("text/html;charset=utf-8");
       String jsonString = null;
       if (JSONUtils.isArray(data)) {
           JSONArray json = JSONArray.fromObject(data,JSONCOFIG);
           jsonString = json.toString();
       } else {
           JSONObject json = JSONObject.fromObject(data,JSONCOFIG);
           jsonString = json.toString();
       }
//       LOG.info("JSON:"+jsonString);
       try {
			response.getWriter().print(jsonString);
		} catch (IOException e) {
			LOG.error(e);
			e.printStackTrace();
		}
   }
    public void execute(MyContext context,HttpServletResponse response,Object data){
      this.execute(response, data);
    }
}
